package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.Calendar;

public class MassageActivity1 extends AppCompatActivity implements View.OnClickListener {
    ImageButton btn_img_avatar;
    TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7;
    Button btn_save,btn_clear,btn_birthday;
    RadioButton rb_male,rb_female;
    EditText ed_name,ed_choose,ed_address,ed_ct,ed_sn,ed_like,ed_birthday;
    int[] img_avatars = new int[]{
            R.drawable.touxiang1,
            R.drawable.touxiang2
    };
    int i=0,j=0;
    String info = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        showDateOnClick(ed_birthday);
        btn_img_avatar();
        rb_male.setOnClickListener(this);
        rb_female.setOnClickListener(this);
        btn_save();
        btn_clear();
    }
    private void btn_save() {
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //tv_choose.setText(info);
                //封装尝试
//                Bundle b = new Bundle();
//                String name = ed_name.getText().toString();
//                int img = img_avatars.length;
//                b.putString("name", String.valueOf(ed_name));
//                b.putString("img", String.valueOf(j));
//                Intent intent = new Intent(MainActivity2.this, MainActivity.class);
//                intent.putExtras(b);
//                startActivity(intent);
                Intent intent = new Intent();
                String value1,value2,value3,value4,value5,value6,value7;
                value1=ed_name.getText().toString().trim();
                value2=ed_choose.getText().toString().trim();
                value3=ed_address.getText().toString().trim();
                value4=ed_birthday.getText().toString().trim();
                value5=ed_ct.getText().toString().trim();
                value6=ed_sn.getText().toString().trim();
                value7=ed_like.getText().toString().trim();
                intent.putExtra("image",j);
                intent.putExtra("name",ed_name.getText().toString().trim());
                intent.putExtra("gender",ed_choose.getText().toString().trim());
                //intent.putExtra("sex",0);
                //intent.putExtra("gender",ed_choose.setText(info).onClick());
                intent.putExtra("address",ed_address.getText().toString().trim());
                intent.putExtra("birthday",ed_birthday.getText().toString().trim());
                intent.putExtra("constellation",ed_ct.getText().toString().trim());
                intent.putExtra("signature",ed_sn.getText().toString().trim());
                intent.putExtra("favorite",ed_like.getText().toString().trim());
                intent.setClass(MassageActivity1.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }
    //更换头像
    private void btn_img_avatar() {
        btn_img_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                i=i%2;
                switch (i)
                {
                    case 0:btn_img_avatar.setBackgroundResource(img_avatars[i]);j=1;break;
                    case 1:btn_img_avatar.setBackgroundResource(img_avatars[i]);j=1;break;
                }
            }
        });
    }

    private void showDateOnClick(EditText ed_birthday) {
        btn_birthday.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    showDatePickDlg(ed_birthday);
                    return true;
                }

                return false;
            }
        });
        //改变日期
        btn_birthday.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    showDatePickDlg(ed_birthday);
                }
            }
        });
    }
    private void showDatePickDlg(EditText ed_birthday) {
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(MassageActivity1.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                ed_birthday.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }
    private void initView() {
        btn_img_avatar=findViewById(R.id.imageButton_avatar);
        rb_male=findViewById(R.id.radioButton_male);
        rb_female=findViewById(R.id.radioButton_female);
        ed_choose=findViewById(R.id.editText_choose_sex);
        ed_birthday=findViewById(R.id.editText_birthday);
        //img_avatar=findViewById(R.id.imageView_avatar);
        tv1=findViewById(R.id.textView_name);
        tv2=findViewById(R.id.textView_sex);
        tv3=findViewById(R.id.textView_address);
        tv4=findViewById(R.id.textView_birthday);
        tv5=findViewById(R.id.textView_constellation);
        tv6=findViewById(R.id.textView_signature);
        tv7=findViewById(R.id.textView_favorite);
        ed_name=findViewById(R.id.editText_name2);
        ed_address=findViewById(R.id.editText_address);
        ed_ct=findViewById(R.id.editText_constellation);
        ed_sn=findViewById(R.id.editText_signature);
        ed_like=findViewById(R.id.editText_favorite);
        btn_birthday=findViewById(R.id.button_birthday);
        btn_save=findViewById(R.id.button_save);
        btn_clear=findViewById(R.id.button_clear);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.radioButton_male:
                info = rb_male.getText().toString().trim();
                break;
            case R.id.radioButton_female:
                info = rb_female.getText().toString().trim();
                break;
        }
        ed_choose.setText(info);
    }
    private void btn_clear() {
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ed_name.setText("");
                ed_choose.setText("");
                ed_address.setText("");
                ed_birthday.setText("");
                ed_ct.setText("");
                ed_sn.setText("");
                ed_like.setText("");
            }
        });
    }
}